<template>
  <n-card :bordered="false" class="proCard">
    <BasicForm @register="register" @submit="handleSubmit" @reset="handleReset">
      <template #statusSlot="{ model, field }">
        <n-input v-model:value="model[field]" />
      </template>
    </BasicForm>

    <BasicTable
      :columns="columns"
      :request="loadDataTable"
      :row-key="(row:ListData) => row.id"
      ref="actionRef"
      @update:checked-row-keys="onCheckedRow"
      :scroll-x="1090"
    />
  </n-card>
</template>

<script lang="ts" setup>
  import { h, reactive, ref } from 'vue';
  import { BasicTable, TableAction } from '@/components/Table';
  import { BasicForm, FormSchema, useForm } from '@/components/Form/index';
  import { getEmailTableList } from '@/api/table/list';
  import { order_columns, track_columns, card_columns, code_columns, ListData } from './columns';
  import { PlusOutlined } from '@vicons/antd';
  import { useRouter } from 'vue-router';
  import { type FormRules } from 'naive-ui';

  const schemas: FormSchema[] = [
    {
      field: 'user',
      component: 'NInput',
      label: '邮箱账号',
      componentProps: {
        placeholder: '请输入邮箱账号',
        onInput: (e: any) => {
          console.log(e);
        },
      },
      rules: [{ required: true, message: '请输入邮箱账号', trigger: ['blur'] }],
    },
    {
      field: 'pwd',
      component: 'NInput',
      label: '邮箱密码',
      componentProps: {
        placeholder: '请输入邮箱密码',
        onInput: (e: any) => {
          console.log(e);
        },
      },

      rules: [{ required: true, message: '请输入邮箱密码', trigger: ['blur'] }],
    },
    {
      field: 'type',
      component: 'NSelect',
      label: '类型',
      componentProps: {
        placeholder: '请选择类型',
        options: [
          {
            label: '查询验证码',
            value: 'code',
          },
          {
            label: '查询礼品卡',
            value: 'card',
          },
          {
            label: '查询订单状态',
            value: 'order',
          },
          {
            label: '查询物流单号',
            value: 'track',
          },
        ],
        onUpdateValue: (e: any) => {
          console.log(e);
        },
      },
    },
  ];

  const router = useRouter();
  const formRef: any = ref(null);
  const actionRef = ref();
  const columns = ref(code_columns);
  const showModal = ref(false);
  const formBtnLoading = ref(false);

  const [register, { getFieldsValue }] = useForm({
    gridProps: { cols: '1 s:1 m:2 l:3 xl:4 2xl:4' },
    labelWidth: 200,
    schemas,
  });

  function addTable() {
    showModal.value = true;
  }

  const loadDataTable = async (res) => {
    const { type } = getFieldsValue();
    switch (type) {
      case 'code':
        columns.value = code_columns;
        break;
      case 'card':
        columns.value = card_columns;
        break;
      case 'order':
        columns.value = order_columns;
        break;
      case 'track':
        columns.value = track_columns;
        break;
    }
    return await getEmailTableList({ ...getFieldsValue(), ...res });
  };

  function onCheckedRow(rowKeys) {
    console.log(rowKeys);
  }

  function reloadTable() {
    actionRef.value.reload();
  }

  function handleSubmit(values: Recordable) {
    if (Object.keys(getFieldsValue()).length == 0) return;
    reloadTable();
  }

  function handleReset(values: Recordable) {
    const { type } = getFieldsValue();
    switch (type) {
      case 'code':
        columns.value = code_columns;
        break;
      case 'card':
        columns.value = card_columns;
        break;
      case 'order':
        columns.value = order_columns;
        break;
      case 'track':
        columns.value = track_columns;
        break;
    }
  }
</script>

<style lang="less" scoped></style>
